<?php

namespace catchAdmin\project\controller;

use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\ProjectSatisfactionAnswer;

class SatisfactionAnswer extends CatchController
{
    protected $satisfactionAnswer;

    public function __construct(ProjectSatisfactionAnswer $satisfactionAnswer)
    {
        $this->satisfactionAnswer = $satisfactionAnswer;
    }

    /**
     * 列表
     *
     * @time 2020年01月09日
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(): \think\response\Json
    {
        return CatchResponse::paginate($this->satisfactionAnswer->getList());
    }

    /**
     * 保存
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function save(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        //是否存在数据
        $is_existence = $this->satisfactionAnswer->getOneData(['deleted_at' => 0], 'value', 'created_at');

        //存在数据，在原code上+1
        if ($is_existence)
        {
            $req["value"] = $is_existence['value']+1;
        }else{
            $req["value"] = 1;
        }

        return CatchResponse::success($this->satisfactionAnswer->addData($req));
    }

    /**
     * 更新
     *
     * @time 2020年01月09日
     * @param $id
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success($this->satisfactionAnswer->saveData($id, $request->param()));
    }

    /**
     * 删除
     *
     * @time 2020年01月09日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json
    {
        return CatchResponse::success($this->satisfactionAnswer->destroy($id));
    }

    /**
     * 获取全部数据
     * @return \think\response\Json
     */
    public function get_satisfaction_answer(): \think\response\Json
    {
        return CatchResponse::success($this->satisfactionAnswer->getWholeData([], 'value,label', 'created_at'));
    }
}